<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/10/17
 * Time: 14:48
 */

namespace app\src\sellerCategory\logic;
use app\src\base\logic\BaseLogic;
use app\src\seller\logic\SellerLogic;
use app\src\seller\model\Seller;
use app\src\sellerCategory\model\SellerCategory;

class SellerCategoryLogic extends BaseLogic
{
    protected function _init()
    {
        $this->setModel(new SellerCategory());
    }

    /**
     * 添加类目
     * @param $name
     * @param $parent_id
     * @param string $note
     * @return array
     */
    public function addCategory($name, $parent_id, $note = '', $display_order = 0, $icon = 0)
    {
        if($parent_id > 0){
            $result = $this->getSellerCategoryInfo($parent_id);
            if(!$result['status']){
                return $this->apiReturnErr('parent category does not exist');
            }
            $parent_info = $result['info'];
            $level = ++$parent_info['level'];
            if($level > 3) return $this->apiReturnErr('暂时只支持三级类目');
        }else{
            $level = 1; //顶级类目等级1
        }

        $entity = [
            'name' => $name,
            'parent_id' => $parent_id,
            'note' => $note,
            'level' => $level,
            'display_order' => $display_order,
            'icon' => $icon,
            'create_time' => NOW_TIME,
            'status' => 0
        ];

        $result = $this->add($entity);
        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 获取类目信息
     * @param $category_id
     * @return array
     */
    public function getSellerCategoryInfo($category_id)
    {
        $map = ['id' => $category_id, 'status' => 0];
        $result = $this->getInfo($map);
        if($result['status'] && $result['info']){
            return $this->apiReturnSuc($result['info']);
        }else{
            return $this->apiReturnErr('no such category');
        }
    }

    /**
     * 更新类目信息
     * @param $category_id
     * @param bool $name
     * @param bool $parent_id
     * @param bool $note
     * @return array
     */
    public function updateCategory($category_id, $name = false, $parent_id = false, $note = false, $display_order = false, $icon = false)
    {
        $result = $this->getSellerCategoryInfo($category_id);
        if(!$result['status']) return $this->apiReturnErr($result['info']);

        $data = [];
        if($parent_id !== false){
            if($parent_id > 0) {
                $result = $this->getSellerCategoryInfo($parent_id);
                if(!$result['status']) return $this->apiReturnErr('no such parent category');
                $data['parent_id'] = $parent_id;
                $data['level'] = $result['info']['level'] + 1;
            }else{
                $data['parent_id'] = 0;
                $data['level'] = 1;
            }
        }
        if($name !== false){
            $data['name'] = $name;
        }
        if($note !== false){
            $data['note'] = $note;
        }
        if($display_order !== false){
            $data['display_order'] = $display_order;
        }
        if($icon !==false){
            $data['icon'] = $icon;
        }
        $result = $this->save(['id' => $category_id], $data);
        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 类目全部子级
     * @param $level
     * @param int $parent_id
     * @return array
     */
    public function queryCategory($parent_id = 0, $level = false)
    {
        $map = [
            'parent_id' => $parent_id,
            'status' => 0
        ];
        if($level !== false){
            if($level < 0 || !is_numeric($level)) return $this->apiReturnErr('level 必须大于0的整数');
            $map['level'] = $level;
        }

        $order = 'display_order desc';
        $result = $this->queryNoPaging($map, $order);
        if($result['status']){
            return $this->apiReturnSuc($result['info']);
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 全部类目，暂时2级
     * @return array
     */
    public function queryAllCategory()
    {
        $result = $this->queryCategory();
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        $level0 = $result['info'];
        $category = [];
        foreach ($level0 as $val){
            $result = $this->queryCategory($val['id']);
            $children = [];
            if($result['status']){
                foreach ($result['info'] as $val2){
                    $level3 = $this->queryCategory($val2['id']);
                    $children2 = [];
                    $count2 = 0;
                    foreach($level3['info'] as $val3){

                        $count3 = Seller::where(['category_id'=>$val3['id'],'status'=>0])->count();
                        $count2 += $count3;
                        $children2[] = [
                            'id' => $val3['id'],
                            'name' => $val3['name'],
                            'parent_id' => $val3['parent_id'],
                            'level' => $val3['level'],
                            'icon' => $val3['icon'],
                            'display_order' => $val3['display_order'],
                            'note' => $val3['note'],
                            'count' => $count3
                        ];
                    }
                    $children[] = [
                        'id' => $val2['id'],
                        'name' => $val2['name'],
                        'parent_id' => $val2['parent_id'],
                        'level' => $val2['level'],
                        'icon' => $val2['icon'],
                        'display_order' => $val2['display_order'],
                        'note' => $val2['note'],
                        'count' => $count2,
                        'children' => $children2

                    ];


                }
            }
            $category[] = [
                'id' => $val['id'],
                'name' => $val['name'],
                'parent_id' => $val['parent_id'],
                'level' => $val['level'],
                'display_order' => $val['display_order'],
                'note' => $val['note'],
                'children' => $children
            ];
        }
        return $this->apiReturnSuc($category);
    }

    /**
     * 商家修改类目（仅限最底层类目哦，比如目前二级类目）
     * @param $seller_id
     * @param $category_id
     * @return array
     */
    public function addSeller($seller_id, $category_id)
    {
        $result = $this->getSellerCategoryInfo($category_id);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        $category_info = $result['info'];
        if($category_info['level'] == 0) return $this->apiReturnErr('can not be top category');
        $result = (new SellerLogic)->getSellerInfo($seller_id);
        if(!$result['status']) return $this->apiReturnErr($result['info']);

        $data = [
            'category_id' => $category_id,
            'level' => $category_info['level']
        ];
        $result = (new SellerLogic)->updateSeller($seller_id, $data);
        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 返回分类结构
     * @param $category_id
     * @return array
     */
    public function getFullCategoryName($category_id)
    {
        $result = $this->getSellerCategoryInfo($category_id);
        if(!$result['status']) return $this->apiReturnErr($result['info']);

        $category_info = $result['info'];
        $level = $category_info['level'];
        $nameList = [$category_info['name']];
        if($level > 1) {
            for($i = $level; $i > 1; $i--){
                $result = SellerCategory::get(['id' => $category_info['parent_id']]);
                if(is_null($result)) break;
                $category_info = $result;
                array_unshift($nameList, $result['name']);
            }
        }
        return $this->apiReturnSuc(implode($nameList, ' - '));
    }
}